package com.example.archermind.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GatewaySystemActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.btn_gatewaysystembinder)
    Button btnGatewaysystembinder;
    @BindView(R.id.btn_beginLog)
    Button btnBeginLog;
    @BindView(R.id.btn_endLog)
    Button btnEndLog;
    @BindView(R.id.btn_onProximitySensorChanged)
    Button btnOnProximitySensorChanged;
    @BindView(R.id.btn_registerGatewaySystemCallbackCallback)
    Button btnRegisterGatewaySystemCallbackCallback;
    @BindView(R.id.btn_unregisterGatewaySystemCallbackCallback)
    Button btnUnregisterGatewaySystemCallbackCallback;
    @BindView(R.id.tv_onProximitySensorChanged)
    TextView tvOnProximitySensorChanged;
    @BindView(R.id.rb_onlinemusic)
    RadioButton rbOnlinemusic;
    @BindView(R.id.rb_onlineradio)
    RadioButton rbOnlineradio;
    @BindView(R.id.rb_onlinemedia)
    RadioButton rbOnlinemedia;
    @BindView(R.id.rb_weather)
    RadioButton rbWeather;
    @BindView(R.id.rg_openAndroidApp)
    RadioGroup rgOpenAndroidApp;
    @BindView(R.id.cb_fullscreen)
    CheckBox cbFullscreen;
    @BindView(R.id.btn_openAndroidApp)
    Button btnOpenAndroidApp;
    @BindView(R.id.tv_openAndroidApp)
    TextView tvOpenAndroidApp;
    @BindView(R.id.btn_closeAndroidApp)
    Button btnCloseAndroidApp;
    @BindView(R.id.tv_closeAndroidApp)
    TextView tvCloseAndroidApp;
    private String action;

    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle bundle = msg.getData();
            String action = bundle.getString("action");
            int result = bundle.getInt("result");

            switch (msg.what){
                case GATEWAYSYSTEMCALLBACK:
                    tvOpenAndroidApp.setText("OpenAndroidApp action:"+action+" result:"+result);
                    break;
                case GATEWAYSYSTEMCALLBACK+1:
                    tvCloseAndroidApp.setText("CloseAndroidApp action:"+action+" result:"+result);
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gateway_system);
        unbinder = ButterKnife.bind(this);
        setHandler(mHandler);

    }

    @Override
    @OnClick({R.id.btn_gatewaysystembinder, R.id.btn_beginLog, R.id.btn_endLog, R.id.btn_onProximitySensorChanged,
            R.id.btn_registerGatewaySystemCallbackCallback, R.id.btn_unregisterGatewaySystemCallbackCallback,
            R.id.btn_openAndroidApp,R.id.btn_closeAndroidApp,
            R.id.rg_openAndroidApp,
            R.id.rb_onlinemedia,R.id.rb_onlinemusic,R.id.rb_onlineradio,R.id.rb_weather,
            R.id.cb_fullscreen})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_gatewaysystembinder:
                getGatewaySystemService();
                break;
            case R.id.btn_beginLog:
                try {
                    getGatewaySystemService().beginLog();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_endLog:
                try {
                    getGatewaySystemService().endLog();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_onProximitySensorChanged:
                try {
                    getGatewaySystemService().onProximitySensorChanged((float) 1.0);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_registerGatewaySystemCallbackCallback:
                try {
                    getGatewaySystemService().registerGatewaySystemCallbackCallback(getGatewaySystemCallback());
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

                break;
            case R.id.btn_unregisterGatewaySystemCallbackCallback:
                try {
                    getGatewaySystemService().unregisterGatewaySystemCallbackCallback(getGatewaySystemCallback());
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.rg_openAndroidApp:
                break;
            case R.id.rb_onlinemedia:
            case R.id.rb_onlinemusic:
            case R.id.rb_onlineradio:
            case R.id.rb_weather:
                action = ((RadioButton)view).getText().toString();
                break;
            case R.id.btn_closeAndroidApp:
                if(TextUtils.isEmpty(action)){
                    Toast.makeText(this, "请选择需要打开的程序", Toast.LENGTH_SHORT).show();
                    return;
                }
                try {
                    getGatewaySystemService().closeAndroidApp(action);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_openAndroidApp:
                if(TextUtils.isEmpty(action)){
                    Toast.makeText(this, "请选择需要打开的程序", Toast.LENGTH_SHORT).show();
                    return;
                }
                try {
                    getGatewaySystemService().openAndroidApp(action,cbFullscreen.isChecked());
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;

        }
    }
}
